package weixinkeji.dev.dc.base.food.h.food.common;

import MyUI.ligerUI.bean.LigerUIOrderBy;
import MyUI.ligerUI.service.LigeruiService;
import com.alibaba.fastjson.JSON;
import sys.wx.c.ATR;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.c.components.cv.CV;
import sys.wx.m.db.SQL.mysql.common.TJF;
import sys.wx.m.db.SQL.mysql.common.orderby.OrderBy;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.SQL.mysql.common.where.WhereConfig;
import sys.wx.m.db.dao.mysql.Dao;
import weixinkeji.dev.dc.base.food.bean.ViewFood;
import weixinkeji.dev.dc.base.food.sql.FoodSQL;

/**
 *
 * @author IK
 */
@H("view/show/food")
public class ViewFoodS {

    @M("/viewfood")//view/show/food/viewfood.imy
    public static String s(ATR a) { //weixinkeji/dev/dc/base/unit/s/manager.imy
//        LigeruiServies ls=new LigeruiServices();
        LigerUIOrderBy page = LigeruiService.getOrderBy(a.request);

        a.printOne(
                JSON.toJSONString(Dao.selectVast_My(ViewFood.class, FoodSQL.FOOD_VIEW + getWhere(a) + (null == page.sortname ? "" : getOrderBy(page)))
                ));
        return null;
    }

    private static String getWhere(ATR a) {
        String nameOrCode = CV.getParameter(a.request, "nameOrCode");
        WhereConfig wc = new WhereConfig("F.");

        if (null != nameOrCode) {
            Where wh = TJF.getWhere(wc);
            String likeValue = nameOrCode.trim().isEmpty() ? "" : "'%" + nameOrCode + "%'";
            return wh.put(Where.TJ_IGNORE_EMPTY, Where.AND, "food_code", "LIKE", likeValue)
                    .put(Where.TJ_IGNORE_EMPTY, Where.OR, "food_name", "LIKE", likeValue)
                    .getWhereTakeWhere_AotuRemove0ANDOR();
//
//            return nameOrCode.isEmpty() ? ""
//                    : "WHERE " + wh.put("food_code", "LIKE", likeValue).OR().put("food_name", "LIKE", likeValue).getWhere();
        }
        String id = CV.getParameter(a.request, "series_id");
        return null == id ? "" : "WHERE " + Where.putIN(Where.TJ_IGNORE_NOTHING, null, "S.", "series_id", CV.toSQLStringValue(id));
    }

    private static String getOrderBy(LigerUIOrderBy page) {
        OrderBy ob = TJF.getOrderBy("F.");
        switch (page.sortname) {
            case "unit_name":
                ob.put("U.", "unit_name").putASC_DESC(page.sortorder);
                break;
            case "series_name":
                ob.put("S.", "series_name").putASC_DESC(page.sortorder);
                break;
            default:
                ob.put(page.sortname).putASC_DESC(page.sortorder);
        }
        return ob.toSql();
    }

}
